package edu.ccut.saturn.basecomponent.database.impl.sqlserver.builder.unused;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.impl.AbstractSelectSQLBuilder;
import edu.ccut.saturn.basecomponent.database.impl.SaturnSQL;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.SaturnDataManager;

public class OracleSelectSQLBuilder extends AbstractSelectSQLBuilder {

	@Override
	protected String getDatabaseType() {
		return SaturnSQLBuilderFactory.ORACLE;
	}

	@Override
	protected String getOperationType() {
		return ISaturnSQL.SELECT_OPERATION;
	}
	
	/**
	 * 重写分页逻辑
	 */
	public ISaturnSQL build(String fullKey, String condition, int startIndex,
			int endIndex) throws Exception {
		return this.build(fullKey, condition, null, startIndex, endIndex);
	}
	/**
	 * 重写分区表分页逻辑
	 */
	public ISaturnSQL build(String fullKey, String condition,String tableWord, int startIndex,
			int endIndex) throws Exception {
		IDataInfo dataInfo = SaturnDataManager.getInstance().getSaturnDataInfo(
				fullKey);
		StringBuffer sql = new StringBuffer();
		sql.append(" ");
		String tempSQL = dataInfo.getSqlStr();
		if (tempSQL != null && !"".equals(tempSQL)) {
			sql
					.append(" SELECT * FROM(SELECT A.*,ROWNUM rowno FROM( ");
			sql.append(tempSQL);
			if (condition != null) {
				sql.append(condition.replaceFirst("WHERE", " AND"));
			}
			sql.append(") A) B WHERE rowno BETWEEN ").append(startIndex).append(" AND ").append(endIndex);
		} else {
			// 判断是否多表
			// if (isMultiQuery(dataInfo)) {
			sql
					.append(" SELECT * FROM(SELECT ROWNUM rowno,");
			for (String key : dataInfo.keySet()) {
				IDataItem iDataItem = dataInfo.getDataItem(key);
				String colum = iDataItem.getColumn();
				String columnAlias = iDataItem.getColumnAlias();
				String tableName1 = iDataItem.getTableName();
				// 如果没有表名，即自定义item,就不拼sql
				if (tableName1 != null && !"".equals(tableName1.trim())) {
					sql.append(tableName1).append(".").append(colum);
					if (columnAlias != null && !columnAlias.equals("")) {
						sql.append(" AS ").append(columnAlias).append(",");
					} else {
						sql.append(",");
					}
				}
			}
			sql.deleteCharAt(sql.length() - 1);
			sql.append(" FROM ");
		    //传入表名为空
			if(tableWord!=null && !"".equals(tableWord.trim())){
				sql.append(tableWord);
			}else{
				List<String> tableNamelist = new ArrayList<String>();
				for (String key : dataInfo.keySet()) {
					IDataItem iDataItem = dataInfo.getDataItem(key);
					String tableName1 = iDataItem.getTableName();
					if (tableName1 != null && !"".equals(tableName1.trim())) {
						if (!tableNamelist.contains(tableName1)) {
							tableNamelist.add(tableName1);
						}
					}
				}
				for (String tableName1 : tableNamelist) {
					sql.append(tableName1).append(",");
				}
				sql.deleteCharAt(sql.length() - 1);
			}
			if (condition != null) {
				sql.append(" ").append(condition);
			}
			sql.append(") A WHERE rowno BETWEEN ").append(startIndex).append(
			" AND ").append(endIndex);
		}

		SaturnSQL saturnsql = new SaturnSQL();
		saturnsql.setSql(sql.toString());
		saturnsql.setDBtype(this.getDatabaseType());
		saturnsql.setSaturnData(null);
		saturnsql.setSaturnDataFullKey(dataInfo.getDataFullKey());
		saturnsql.setOperationType(this.getOperationType());

		return saturnsql;
	}
}
